##  SensorState contains the functionality behind the SensorPanel.
class SensorState:
    ##  The constructor of SensorState.
    #   @param[in]  main    Main wx.App derived instance.
    def __init__(self, main):
        ##  Main wx.App derived instance.
        self.main = main
        ##  The sensor rulebase. main.config is an instantiation of the ConfigfileReader class.
        self.sensorRB = self.main.config.ReadSensorRulebase()

    ##  Get dictionary mapping from GSI to position string.
    #   This is derived from the currently instantiated list of sensors,
    #   so as to give feedback what was actually done with the information in the config file.
    #   @return[dictionary] A (key,value) mapping from sensor name to position, e.g. {'fob0_3', 'Thorax'}.
    def get_sensor_positions(self):
        gp_dict = {}
        for sensor in self.main.sensors:
            gp_dict[sensor.ID] = sensor.name

        return gp_dict

    ##  Checks the sensors and their positions compared to eachother using the rulebase.
    #   @return[boolean]    True if all the rules are true, else False.
    def CheckSensors(self):
        # Update the positions of all the sensors.
        # self.main.driver is an instantiation of the FoBDriver class.
        self.main.driver.SampleSensors()

        # Create a dictionary of sensors to pass it to the rules.
        sensors_dict = {}
        for sensor in self.main.sensors:   sensors_dict[sensor.ID] = sensor.position

        # Check each rule in the rulebase.
        for rule in self.sensorRB:
            if not rule.IsTrue(sensors_dict):   return False
        return True